package com.yjhb.android.sbz.view.drawable

import android.graphics.*
import android.graphics.drawable.Drawable
import com.jojo.android.zxlib.tool.ZxTool


/**
 *  创建者 zx
 *  创建时间 2021/4/6 16:24
 *  绘制序号
 */
class NumberDrawable() : Drawable() {
    var number = 1
    set(value) {
        field = value
        invalidateSelf()
    }
    var bgColor = Color.RED
    var textColor = Color.WHITE
    var textSize = ZxTool.sp2px(11F)
    private val paint = Paint()

    init {
        paint.isAntiAlias = true
        paint.style = Paint.Style.FILL_AND_STROKE
        paint.textSize = textSize
        paint.textAlign = Paint.Align.CENTER

    }

    override fun draw(canvas: Canvas) {
        paint.color = bgColor
        canvas.drawCircle((bounds.width() / 2).toFloat(), (bounds.height() / 2).toFloat(), (bounds.width() / 2).toFloat(), paint)
        paint.color = textColor
        val fontMetrics = paint.fontMetrics
        //文字高度
        val fontHeight = fontMetrics.bottom - fontMetrics.top
        val textBase = bounds.height() - (bounds.height() - fontHeight) / 2 - fontMetrics.bottom
        canvas.drawText(number.toString(), (bounds.width() / 2).toFloat(), textBase, paint)

    }

    override fun setAlpha(alpha: Int) {

    }

    override fun getOpacity(): Int {
        return PixelFormat.OPAQUE
    }

    override fun setColorFilter(colorFilter: ColorFilter?) {
        paint.colorFilter = colorFilter
    }
}